/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmPesquisaEvento.java
 *
 * Created on 02/06/2011, 06:46:43
 */

package br.hostess.view;

import br.hostess.controller.cntrManterEvento;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Marcelo Daibert
 */
public class frmPesquisaEvento extends javax.swing.JFrame {

    /** Creates new form frmPesquisaEvento */
    public frmPesquisaEvento() {
        initComponents();
        Dimension objDim = this.getToolkit().getScreenSize();
        Rectangle abounds = this.getBounds();
        this.setLocation((objDim.width - abounds.width) / 2, (objDim.height - abounds.height) / 2);
        this.requestFocus();
        alimentaTabela();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu = new javax.swing.JPopupMenu();
        jMenuItemExcluir = new javax.swing.JMenuItem();
        jPanelPrincipal = new javax.swing.JPanel();
        jScrollPaneTabela = new javax.swing.JScrollPane();
        jTableTabela = new javax.swing.JTable();
        jPanelOpcao = new javax.swing.JPanel();
        jButtonFechar = new javax.swing.JButton();
        jButtonAdicionarNovo = new javax.swing.JButton();

        jMenuItemExcluir.setMnemonic('E');
        jMenuItemExcluir.setText("Excluir");
        jMenuItemExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExcluirActionPerformed(evt);
            }
        });
        jPopupMenu.add(jMenuItemExcluir);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Busca - Eventos");
        setAlwaysOnTop(true);
        setResizable(false);

        jTableTabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableTabela.setComponentPopupMenu(jPopupMenu);
        jScrollPaneTabela.setViewportView(jTableTabela);

        jPanelOpcao.setBorder(javax.swing.BorderFactory.createTitledBorder("Opções"));

        jButtonFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/exit.png"))); // NOI18N
        jButtonFechar.setText("Fechar");
        jButtonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharActionPerformed(evt);
            }
        });

        jButtonAdicionarNovo.setText("Adicionar Novo...");
        jButtonAdicionarNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAdicionarNovoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelOpcaoLayout = new javax.swing.GroupLayout(jPanelOpcao);
        jPanelOpcao.setLayout(jPanelOpcaoLayout);
        jPanelOpcaoLayout.setHorizontalGroup(
            jPanelOpcaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelOpcaoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonAdicionarNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 512, Short.MAX_VALUE)
                .addComponent(jButtonFechar)
                .addContainerGap())
        );
        jPanelOpcaoLayout.setVerticalGroup(
            jPanelOpcaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelOpcaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelOpcaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonFechar)
                    .addComponent(jButtonAdicionarNovo))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanelPrincipalLayout = new javax.swing.GroupLayout(jPanelPrincipal);
        jPanelPrincipal.setLayout(jPanelPrincipalLayout);
        jPanelPrincipalLayout.setHorizontalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelOpcao, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPaneTabela, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 746, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanelPrincipalLayout.setVerticalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneTabela, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelOpcao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

     private void alimentaTabela(){
        @SuppressWarnings("UseOfObsoleteCollectionType")
        Vector<String> vetColunas = new Vector<String>();
        vetColunas.addElement("OID");
        vetColunas.addElement("Descrição");
        DefaultTableModel objTableModel = new DefaultTableModel(vetColunas,0);
        objTableModel = cntrManterEvento.recuperaObjetos(objTableModel);
        jTableTabela.setModel(objTableModel);
    }

    private void jButtonAdicionarNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAdicionarNovoActionPerformed
        new frmManterEvento().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButtonAdicionarNovoActionPerformed

    private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonFecharActionPerformed

    private void jMenuItemExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExcluirActionPerformed
        String vSelecionado = String.valueOf(jTableTabela.getSelectedRow());
        String vIDSelecionado = String.valueOf(jTableTabela.getValueAt(Integer.parseInt(vSelecionado), 0));
        if (cntrManterEvento.excluir(vIDSelecionado)){
            JOptionPane.showMessageDialog(this, "Excluído com Sucesso", "Mensagem de Sistema", JOptionPane.INFORMATION_MESSAGE);
            this.alimentaTabela();
        }else{
            JOptionPane.showMessageDialog(this, "Erro ao Excluir.\n\nAteção: Caso haja participantes cadastrados neste evento, não é possível a exclusão.", "Mensagem de Sistema", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jMenuItemExcluirActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdicionarNovo;
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JMenuItem jMenuItemExcluir;
    private javax.swing.JPanel jPanelOpcao;
    private javax.swing.JPanel jPanelPrincipal;
    private javax.swing.JPopupMenu jPopupMenu;
    private javax.swing.JScrollPane jScrollPaneTabela;
    private javax.swing.JTable jTableTabela;
    // End of variables declaration//GEN-END:variables

}
